﻿#include "CBird.h"
USING_NS_CC;

CBird::CBird():
	m_flocalTime(0),
	m_fInterval(0),
	timer(0),
	m_canUpdate(true)
{
}

CBird::~CBird()
{
}

CBird* CBird::init(BIRD_TYPE newType,BIRD_COLOR newColor,float interval)
{
	CBird* pBird=new CBird();
	pBird->setBird(newType,newColor);
	pBird->m_fInterval=interval;
	pBird->isExplosion = false;
	pBird->canCheckExplosion = true;
	return pBird;
}

void CBird::setBird(BIRD_TYPE newType,BIRD_COLOR newColor)
{
	setType(newType);
	setColor(newColor);
	CCString* pFrameName=CCString::createWithFormat("bird_%d_%d_1.png",m_tColor,m_tType);
	this->initWithSpriteFrameName(pFrameName->getCString());

}
void CBird::setType(BIRD_TYPE type)
{
	m_tType=type;
}

void CBird::setColor(BIRD_COLOR color)
{
	m_tColor=color;
}

int CBird::GetColor()
{
	return (int)m_tColor;
}
void CBird::playAnimation()
{
	CCSpriteFrameCache* cache=CCSpriteFrameCache::sharedSpriteFrameCache();
	CCArray* frameArray=CCArray::createWithCapacity(2);
	for(int i=1;i<3;i++)
	{
		CCString* str=CCString::createWithFormat("bird_%d_%d_%d.png",m_tColor,m_tType,i);
		CCSpriteFrame* frame=cache->spriteFrameByName(str->getCString());
		frameArray->addObject(frame);
	}
	CCAnimation* animation=CCAnimation::createWithSpriteFrames(frameArray,0.3f);
	animation->setRestoreOriginalFrame(true); // set lại frame đầu khi play xong animation
	this->runAction(CCAnimate::create((animation)));
}
void CBird::UpdateAnimation(float dt)
{
	m_flocalTime+=dt;
	if(m_flocalTime >= m_fInterval)
	{
		playAnimation();
		m_flocalTime=0;
	}
}